package gcu.module.room.rest;

import gcu.module.build.daomain.Build;
import gcu.module.build.service.BuildService;
import gcu.module.owner.daomain.Owner;
import gcu.module.owner.service.OwnerService;
import gcu.module.room.daomain.Room;
import gcu.module.room.service.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by haol on 2016/9/26.
 */
@RestController
@RequestMapping("/rest/room")
public class RoomRestController {
      @Autowired
      private RoomService roomService;
      @Autowired
      private BuildService buildService;
      @Autowired
      private OwnerService ownerService;

      @RequestMapping(value = "/get",method = RequestMethod.GET)
      public Map<String,Object> get(Integer id,Integer buildId){
            return roomService.get(id,buildId);
      }

      @RequestMapping(value = "/save", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
      public boolean save(@RequestBody Room room) {
            if (room.getId() == null){
                  roomService.addRoom(room);
            }else {
                  roomService.updateRoom(room);
            }
            return true;
      }

      @RequestMapping(value = "/checkRoomIn",method = RequestMethod.POST)
      public boolean check(String buildName,String roomNum,Integer id){
            if(id != null){
                  Owner owner = ownerService.queryOwnerById(id);
                  if(owner.getBuildName().equals(buildName) &&
                          owner.getRoomNum().equals(roomNum)){
                        return true;
                  }
            }
            Build build = buildService.queryBuildByName(buildName);
            Room room = roomService.queryRoomByNameAndBuildName(build.getBuildName(),roomNum);
            if(room == null){
                  return false;
            }else {
                  if(room.getIsIn() == 1){
                        return false;
                  }else {
                        return true;
                  }
            }
      }
      @RequestMapping(value = "/checkRoomAdd",method = RequestMethod.POST)
      public boolean checkAdd(String buildName,String roomNum){
            Build build = buildService.queryBuildByName(buildName);
            Room room = roomService.queryRoomByNameAndBuildName(build.getBuildName(),roomNum);
            if(room == null){
                  return true;
            }else {
                return false;
            }
      }
      @RequestMapping(value = "/checkRoomIsDel",method = RequestMethod.POST)
      public boolean checkRoomIsDel(String delitems) {
            String[] strs = delitems.split(",");
            for (String str : strs) {
                  int roomId = Integer.parseInt(str);
                  Room room = roomService.queryRoomById(roomId);
                 /* Owner owner = ownerService.queryOwnerByBuildAndRoom(room.getBuildName(),room.getRoomNum());
                  if (owner != null){
                        return false;
                  }*/
                  if(room.getIsIn() == 1){
                        return false;
                  }
            }
            return true;
      }
      @RequestMapping(value = "/delByChecked",method = RequestMethod.POST)
      public String delByChecked(String delitems) {
            List<String> delList = new ArrayList<String>();
            String[] strs = delitems.split(",");
            for (String str : strs) {
                  delList.add(str);
            }
            roomService.deleteRoom(delList);
            return "redirect:/owner/list";
      }
}
